//index.js
//获取应用实例
var app = getApp();
Page({
    data: {
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        loadingHidden: false, // loading
        swiperCurrent: 0,
        categories: [],
        activeCategoryId: 0,
        goods: [],
        scrollTop: "0",
        loadingMoreHidden: true,
        searchInput: '',
        p: 1,
        processing: false,
        total: -1
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: app.globalData.shopName
        });
    },
    onShow: function () {
        const that = this;
        that.getBannerAndCat();
    },
    scroll: function (e) {
        var that = this, scrollTop = that.data.scrollTop;
        that.setData({
            scrollTop: e.detail.scrollTop
        });
    },
    //事件处理函数
    swiperchange: function (e) {
        this.setData({
            swiperCurrent: e.detail.current
        })
    },
	listenerSearchInput:function( e ){
	        this.setData({
	            searchInput: e.detail.value
	        });
	 },
	 toSearch:function( e ){
	        this.setData({
	            p:1,
	            goods:[],
	            loadingMoreHidden:true
	        });
	        this.getFoodList();
	},
    tapBanner: function (e) {
        if (e.currentTarget.dataset.id != 0) {
            wx.navigateTo({
                url: "/pages/food/info?id=" + e.currentTarget.dataset.id
            });
        }
    },
    toDetailsTap: function (e) {
        wx.navigateTo({
            url: "/pages/food/info?id=" + e.currentTarget.dataset.id
        });
    },
    getBannerAndCat: function () {
        const that = this;
        wx.request({
           url: app.buildUrl('/food/index'),
            header: app.getRequestHeader(),
            success: function (res) {
                const resp = res.data;
                if (resp.code === 200) {
                    that.setData({
                        banners: resp.data.banner_list,
                        categories: resp.data.cat_list
                    });
                    that.getFoodList();
                } else {
                    app.alert({'content': resp.msg});
                    return;
                }
            }
        });
    },
    getFoodList: function () {
        const that = this;
        if (that.data.processing) {
            return;
        }
        if (that.data.goods.length === that.data.total) {
            return;
        }
        if (!that.data.loadingMoreHidden) {
            return;
        }
        that.setData({
            processing: true
        });

        wx.request({
            url: app.buildUrl("/food/search"),
            header: app.getRequestHeader(),
            data: {
                cat_id: that.data.activeCategoryId,
                mix_kw: that.data.searchInput,
                p: that.data.p
            },
            success: function (res) {
                const resp = res.data
                if (resp.code === 200) {
                    const goods = resp.data.list;
                    that.setData({
                        goods: that.data.goods.concat(goods),
                        p: that.data.p + 1,
                        processing: false,
                        total: resp.data.total
                    })
                    if (resp.data.has_more === 0) {
                        that.setData({
                            loadingMoreHidden: false
                        })
                    }
                } else {
                    app.alert({"content": resp.msg});
                    return;
                }
            }
        })
    },
    catClick: function (e) {
        console.log(e.currentTarget.id)
        this.setData({
            activeCategoryId: e.currentTarget.id,
            p: 1,
            goods: [],
            loadingMoreHidden: true,
            total: -1
        });
        this.getFoodList();
    },
    onReachBottom: function () {
        const that = this;
        setTimeout(function () {
            that.getFoodList();
        }, 500);
    }
});
